package sz.szsmk.citizencard.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;

import com.youzan.androidsdk.YouzanToken;
import com.youzan.androidsdk.basic.YouzanBrowser;
import com.youzan.androidsdk.event.AbsAuthEvent;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.ShopingFragmentBinding;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonFragmentComponent;
import sz.szsmk.citizencard.model.network.bean.response.YzanResponse;
import sz.szsmk.citizencard.mvp.contract.IShoppingContact;
import sz.szsmk.citizencard.mvp.presenter.ShopingPresenter;
import sz.szsmk.citizencard.ui.base.IBaseFragment;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/6/4 17:04
 * description:
 */
@SuppressLint("ValidFragment")
public class ShopingFragment extends IBaseFragment<ShopingPresenter, ShopingFragmentBinding>
        implements IShoppingContact.IShoppingView {

    private YouzanBrowser shopYouzanWeb;
    private boolean mIsWebViewAvailable;
    private boolean aBoolean;
    private String tokenYouzan;
    private String cookieKey;
    private String cookieValue;


    public static ShopingFragment getInstance() {
        ShopingFragment sf = new ShopingFragment();
        return sf;
    }

    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {

                case 1:
                    break;

                case 0:

                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected int attachLayoutRes() {
        return R.layout.shoping_fragment;
    }

    @Override
    protected void initInjector() {
        DaggerCommonFragmentComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }


    @Override
    protected void initViews() {
        shopYouzanWeb = dataBinding.shopYouzanWeb;
        shopYouzanWeb.loadUrl("https://j.youzan.com/t3waNY");
        shopYouzanWeb.subscribe(new AbsAuthEvent() {
            @Override
            public void call(Context context, boolean b) {

                mPresenter.loginReq();
//                if (aBoolean) {
//                        mPresenter.loginReq();
//                } else if (b) {
////                    Router.build("login").go(getActivity());
//                } else {
//                    mPresenter.unLoginReq();
//                }
            }
        });


        mIsWebViewAvailable = true;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


    }

    @Override
    protected void updateViews() {
        //mPresenter.getUserInfo();


    }

    @Override
    public void updataError() {

    }

    @Override
    public void getUserInfoSucc(YzanResponse response) {
        if (response == null){
            return;
        }
        tokenYouzan = response.getAccess_token_youzan();
        cookieKey = response.getCookie_key();
        cookieValue = response.getCookie_value();
        YouzanToken youzanToken = new YouzanToken();
        youzanToken.setAccessToken(tokenYouzan);
        youzanToken.setCookieKey(cookieKey);
        youzanToken.setCookieValue(cookieValue);
        shopYouzanWeb.sync(youzanToken);
        //L.e(Constant.TAG,"tokenYouzan+cookieKey+cookieValue" + tokenYouzan+"+"+cookieKey);
    }

    @Override
    public void unLoginSucc(YzanResponse response) {
        if (response == null){
            return;
        }
        String accessTokenYouzan = response.getAccess_token_youzan();
        YouzanToken youzanToken = new YouzanToken();
        youzanToken.setAccessToken(accessTokenYouzan);
        shopYouzanWeb.sync(youzanToken);
    }

    @Override
    public void loginSucc() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        handler.removeCallbacksAndMessages(null);
    }
}
